package com.lg.online_shop.controller;

import com.lg.online_shop.enmu.OrderEnum;
import com.lg.online_shop.entity.Color;
import com.lg.online_shop.entity.Orders;
import com.lg.online_shop.entity.Size;
import com.lg.online_shop.mapper.*;
import com.lg.online_shop.utils.Result;
import com.lg.online_shop.vo.CommonChartData;
import com.lg.online_shop.vo.OrderVo;
import com.lg.online_shop.vo.Overview;
import com.mysql.cj.result.Row;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.pattern.PathPattern;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin(origins ="*")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private GoodCategoryMapper goodCategoryMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private GoodMapper goodMapper;

    @Resource
    private SizeMapper sizeMapper;

    @Resource
    private ColorMapper colorMapper;

    @Resource
    private  AddressMapper addressMapper;

    @Resource
    private UserMapper userMapper;



    //    获取概览数据
    @GetMapping("/getOverview")
    public Result getData() {
        Overview overview = new Overview();
//        获取商品种类数量
        Long categoryCount = goodCategoryMapper.selectCount(null);
        overview.setCategoryCount(categoryCount);
//        获取销售总量(status为1的订单数量)
        Long saleCount = orderMapper.selectCountWithPaid();
        overview.setSaleCount(saleCount);
//        获取总营业额
        double money = orderMapper.selectSalemoney();
        overview.setMoney(money);
//        获取用户数量
        Long userCount = goodCategoryMapper.selectCount(null);
        overview.setUserCount(userCount);

//        获取销售数量,按类别统计
        List<CommonChartData> saleCountWithCategory = goodCategoryMapper.selectCountWithCategory();
        overview.setSaleChart(saleCountWithCategory);
//        获取库存数量
        List<CommonChartData> stockCountWithCategory = goodMapper.selectCountWithGood();
        overview.setStockChart(stockCountWithCategory);
        return Result.success(overview);
    }



    //    获取购买历史，即 status 为1 的订单
    @GetMapping("/getBuyHistory")
    public Result getBuyHistory() {
        List<Orders> ordersList = orderMapper.selectList(null);
        List<OrderVo> orderVoList = new ArrayList<>();
//      通过 colorId 和 sizeId 获取颜色和尺码
        for (Orders orders : ordersList) {
            Size size = sizeMapper.selectSizeBySizeId(orders.getSizeId()).get(0);
            Color color = colorMapper.selectColorByColorId(orders.getColorId()).get(0);
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(orders, orderVo);
            orderVo.setSize(size.getSize());
            orderVo.setColor(color.getColor());
            orderVo.setStatus(OrderEnum.fromCode(orders.getStatus()).getDescription());
            orderVo.setAddressName(addressMapper.selectById(orders.getAddressId()).getAddressName());
            orderVo.setAddressee(addressMapper.selectById(orders.getAddressId()).getAddressee());
            orderVo.setPhone(addressMapper.selectById(orders.getAddressId()).getPhone());
            orderVo.setGName(goodMapper.selectById(orders.getGId()).getGName());
            orderVoList.add(orderVo);
        }
        return Result.success(orderVoList);
    }

}
